﻿// #Region FileInfo
// Creater:  程邵磊
// FileName:  DateTimeExtensions.cs
// CreateTime:  2018/10/28
// #EndRegion
// 

using System;

namespace Groove.Core.Infrastructure
{
    /// <summary>
    /// DateTimeExtensions
    /// </summary>
    public static class DateTimeExtensions
    {
        /// <summary>
        /// Total year between two datetime object
        /// </summary>
        /// <param name="start">start datetime</param>
        /// <param name="end">end datetime</param>
        /// <returns></returns>
        public static int? DifferenceTotalYears(this DateTime? start, DateTime end)
        {
            if (!start.HasValue)
                return null;
            var months = (end.Year - start.Value.Year) * 12 + (end.Month - start.Value.Month);
            if (end.Day < start.Value.Day)
            {
                months--;
            }
            return months / 12;
        }

        /// <summary>
        /// Total year between two datetime object
        /// </summary>
        /// <param name="start">start datetime</param>
        /// <param name="end">end datetime</param>
        /// <returns></returns>
        public static int DifferenceTotalYears(this DateTime start, DateTime end)
        {
            var months = (end.Year - start.Year) * 12 + (end.Month - start.Month);
            if (end.Day < start.Day)
            {
                months--;
            }
            return months / 12;
        }

        /// <summary>
        /// Convert datetime to long date string
        /// Format string:yyyy-MM-dd HH:mm:ss
        /// </summary>
        public static string ToLongString(this DateTime dateTime)
        {
            return dateTime.ToString(DateTimeFormatStrings.LongChineseTimeFormatString);
        }

        /// <summary>
        /// Convert datetime to short date string
        /// Format string:yyyy-MM-dd
        /// </summary>
        public static string ToShortString(this DateTime dateTime)
        {
            return dateTime.ToString(DateTimeFormatStrings.ShortTimeFormatString);
        }

        /// <summary>
        /// Convert datetime to long date string
        /// Format string:yyyy年MM月dd日 HH:mm:ss
        /// </summary>
        public static string ToChineseLongString(this DateTime dateTime)
        {
            return dateTime.ToString(DateTimeFormatStrings.LongChineseTimeFormatString);
        }

        /// <summary>
        /// Convert datetime to short date string
        /// Format string:yyyy年MM月dd日
        /// </summary>
        public static string ToChineseShortString(this DateTime dateTime)
        {
            return dateTime.ToString(DateTimeFormatStrings.ShortChineseTimeFormatString);
        }

    }
}